﻿// app/service/user.js
const Service = require('egg').Service;
const SMSClient = require('@alicloud/sms-sdk')
// ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
const accessKeyId = 'LTAINczwarPWWICg'
const secretAccessKey = 'FAUeYSdP7B67Uccn5qBvJ64wVK8o7B'
//初始化sms_client
let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
var sha1 = require("sha1")

class phoneMessage extends Service {

    async message(PhoneNumber) {
        let codeArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        codeArr.sort(function () { return 0.5 - Math.random() })
        codeArr = codeArr.splice(0, 5)
        let code = codeArr.join("")
        let encode = sha1(code + PhoneNumber + "wang_user")
     
        let TemplateParam = `{"code":"${code}"}`
        
        return smsClient.sendSMS({
            PhoneNumbers:PhoneNumber,
            SignName: '考拉去玩',
            TemplateCode: 'SMS_110835150',
            TemplateParam
        }).then(function (res) {
            let {Code} = res
            if (Code === 'OK') {
                
                return  {
                    data: "ok",
                    encode: encode
                }

            }

        })


    }

}

module.exports = phoneMessage;
